package com.cg.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cg.entity.AppMenuEntity;
import com.cg.service.AppMenuService;
import com.cg.utils.Query;
import com.cg.utils.R;
import com.cg.utils.RRException;
import com.cg.utils.UploadUtil;
import com.cg.utils.annotation.SysLog;
import com.cg.utils.xss.XssHttpServletRequestWrapper;

/**
* APP配置
*/
@RestController
@RequestMapping("/app/")
public class AppConfigController {
	@Autowired
	AppMenuService appMenuService;
	@Autowired
	private UploadUtil uploadUtil;
	/**访问地址*/
	@Value("${global.upload.path}")
	private String path;
	/**存放地址*/
	@Value("${global.upload.docBase}")
	private String docBase;
	
	private String mypath="appmenu/";
	
	/**
	 * 所有APP菜单列表
	 */
	@RequestMapping("menu/list")
	@RequiresPermissions("app:menu:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<AppMenuEntity> menuList = appMenuService.queryList(query);
		int total = appMenuService.queryTotal(query);
		
		//PageUtils pageUtil = new PageUtils(menuList, total, query.getLimit(), query.getPage());
		
		return R.success(total, "", menuList);
	}
	
	/**
	 * 保存带图片
	 */
	@SysLog("保存App菜单")
	@RequestMapping("menu/save")
	@RequiresPermissions("app:menu:save")
	public R save(HttpServletRequest request,String url){
		HttpServletRequest req = XssHttpServletRequestWrapper.getOrgRequest(request);
//		List<String> filePath = uploadUtil.upload(req,docBase, path,"appmenu/");
		String filePath = uploadUtil.uploadImg(request,mypath);
		AppMenuEntity menu = new AppMenuEntity();
		String name = req.getParameter("name");
		String code = req.getParameter("code");
		Integer parentId ;
		if(!StringUtils.isBlank(req.getParameter("parentId"))){
			parentId = Integer.parseInt(req.getParameter("parentId"));
			menu.setType(1);
		}else{
			parentId = 0;
			menu.setType(0);
		}
		Integer enable = Integer.parseInt(req.getParameter("enable"));
		menu.setName(name);
		menu.setParentId(parentId);
		menu.setCreateTime(new Date());
		menu.setUpdateTime(new Date());
		menu.setUrl(url);
		menu.setCode(code);
		menu.setEnable(String.valueOf(enable));
		menu.setImgUrl(filePath);
		appMenuService.save(menu);
		return R.ok();
	}
	
	/**
	 * 保存不带图片
	 */
	@SysLog("保存App菜单")
	@RequestMapping("menu/save2")
	@RequiresPermissions("app:menu:save")
	public R save(@RequestBody AppMenuEntity menu){
		//数据校验
		verifyForm(menu);
		if(menu.getParentId()==null){
			menu.setParentId(0);
			menu.setType(0);
		}else{
			menu.setType(1);
		}
		menu.setCreateTime(new Date());
		menu.setUpdateTime(new Date());
		appMenuService.save(menu);
		
		return R.ok();
	}
	
	/**
	 * 修改不带图片
	 */
	@SysLog("修改菜单")
	@RequestMapping("menu/update2")
	@RequiresPermissions("app:menu:update")
	public R update(@RequestBody AppMenuEntity menu){
		//数据校验
		verifyForm(menu);
		if(menu.getParentId()==null){
			menu.setParentId(0);
			menu.setType(0);
		}else{
			menu.setType(1);
		}
		menu.setUpdateTime(new Date());		
		appMenuService.update(menu);
		
		return R.ok();
	}
	
	/**
	 * 修改带图片
	 */
	@SysLog("修改菜单")
	@RequestMapping("menu/update")
	@RequiresPermissions("app:menu:update")
	public R update(HttpServletRequest request){
		HttpServletRequest req = XssHttpServletRequestWrapper.getOrgRequest(request);
//		List<String> filePath = uploadUtil.upload(req,docBase, path,"resource/appmenu/");
		String filePath = uploadUtil.uploadImg(request,mypath);
		AppMenuEntity menu = new AppMenuEntity();
		Integer id = Integer.parseInt(req.getParameter("id"));
		String name = req.getParameter("name");
		String code = req.getParameter("code");
		Integer parentId ;
		if(!StringUtils.isBlank(req.getParameter("parentId"))){
			parentId = Integer.parseInt(req.getParameter("parentId"));
			menu.setType(1);
		}else{
			parentId = 0;
			menu.setType(0);
		}
		Integer enable = Integer.parseInt(req.getParameter("enable"));
		menu.setId(id);
		menu.setName(name);
		menu.setParentId(parentId);
		menu.setCode(code);
		menu.setUpdateTime(new Date());
		menu.setEnable(String.valueOf(enable));
		menu.setImgUrl(filePath);
		appMenuService.update(menu);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除App菜单")
	@RequestMapping("menu/delete")
	@RequiresPermissions("app:menu:delete")
	public R delete(@RequestBody Integer[] menuIds){
		appMenuService.deleteBatch(menuIds);
		
		return R.ok();
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(AppMenuEntity menu){
		if(StringUtils.isBlank(menu.getName())){
			throw new RRException("菜单名称不能为空");
		}
	}

}
